<template>
  <div class="home-box">
    <!-- 顶部导航 -->
    <van-nav-bar fixed class="page-nav-bar">
      <van-button to="/search" class="search-btn" slot="title" icon="search" type="primary">搜索</van-button>
    </van-nav-bar>

    <!-- tab标签页面 -->
    <!-- v-model: 双向绑定 -->
    <van-tabs class="channel-tabs" v-model="active" animated swipeable>
      <van-tab v-for="item in channels" :key="item.id" :title="item.name">
        <!-- 对应的内容: 懒渲染 -->
        <!-- {{ item.id }} -->
        <ArticleList :channelId="item.id" />
      </van-tab>
      <!-- 汉堡菜单 -->
      <div @click="isShowEditChannelPopup = true" slot="nav-right" class="nav-right">
        <i class="toutiao toutiao-gengduo"></i>
      </div>
      <!-- 背后的占位 -->
      <div slot="nav-right" class="placeholder"></div>
    </van-tabs>

    <!-- 编辑频道的弹层 -->
    <van-popup
      v-model="isShowEditChannelPopup"
      closeable
      position="bottom"
      close-icon-position="top-left"
      :style="{ height: '100%' }"
    >
      <ChannelEdit @sendData="getEditChannelData" :active="active" :channels="channels" />
    </van-popup>
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js'
import ArticleList from './components/article-list.vue'
import ChannelEdit from './components/channel-edit.vue'
import { getItem } from '@/utils/storage.js'
export default {
  data () {
    return {
      active: 0,
      channels: [],
      isShowEditChannelPopup: false,
      name: '张三'
    }
  },

  components: {
    ArticleList,
    ChannelEdit
  },

  created () {
    this.loadUserChannels()
  },

  methods: {
    // 加载用户频道数据
    async loadUserChannels () {
      // 判断 用户未登录, 并且 本地没有数据
      const data = getItem('hmtt_channel')
      if (!this.$store.state.user && getItem('hmtt_channel')) {
        this.channels = data
      } else {
        try {
          const res = await getUserChannels()
          this.channels = res.data.data.channels
        } catch (err) {
          this.$toast('获取频道列表失败')
        }
      }
    },
    // 获取子组件的数据的事件函数
    getEditChannelData (e) {
      // console.log(e)
      this.isShowEditChannelPopup = e.close
      this.active = e.active
    }
  }
}
</script>

<style scoped lang='less'>
.home-box {
  padding-bottom: 100px;
  padding-top: 174px;
  .search-btn {
    width: 555px;
    height: 64px;
    background: rgba(255, 255, 255, 0.2);
    border-radius: 32px;
    border: none;
  }

  /deep/.van-icon-search {
    color: #fff;
  }

  /deep/.van-nav-bar__title {
    max-width: unset;
  }

  // 频道tab
  /deep/.channel-tabs {
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 2;
      width: 100%;
    }
    // 默认tab
    .van-tab {
      height: 82px;
      min-width: 200px;
      border-right: 1px solid #edeff3;
      // 文本
      .van-tab__text {
        font-size: 28px;
        color: #777;
      }
    }

    // 选中tab
    .van-tab--active {
      .van-tab__text {
        font-size: 30px;
        color: #333;
      }
    }

    // 选中条
    .van-tabs__line {
      width: 31px;
      height: 6px;
      background: #3296fa;
      border-radius: 3px;
      bottom: 8px;
    }

    // 去除选中条的父盒子的padding值
    .van-tabs__nav {
      padding: 0;
    }

    // tabs右侧的汉堡菜单
    .nav-right {
      width: 66px;
      height: 82px;
      position: fixed;
      right: 0;
      background: rgba(255, 255, 255, 0.9);
      display: flex;
      justify-content: center;
      align-items: center;
      .toutiao {
        font-size: 33px;
      }
      &::before {
        content: "";
        width: 2px;
        height: 82px;
        background-image: url(~@/assets/gradient-gray-line.png);
        position: absolute;
        left: 0;
        background-size: 100% 100%;
      }
    }

    // 右侧汉堡菜单背后的占位
    .placeholder {
      width: 66px;
      height: 82px;
      flex-shrink: 0;
    }
  }
}
</style>
